﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Resn.WhuPub.BLL;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// CreateVisitWindow.xaml 的交互逻辑
        /// </summary>
        public partial class CreateVisitWindow
        {
                private readonly GlobalManager _gm = GlobalManager.Instance;

                public CreateVisitWindow()
                {
                        InitializeComponent();
                }

                private void Window_Loaded(object sender, RoutedEventArgs e)
                {
                        List<VisitType> visitTypes = typeof(VisitType).GetEnums<VisitType>();
                        VisitTypeComboBox.ItemsSource = visitTypes;

                        _gm.Contact = null;
                        _gm.VisitVisitType =visitTypes.First();
                        _gm.VisitEatAmount = 0.0;
                        _gm.VisitGiftAmount = 0.0;
                        _gm.StartTime = DateTime.Now;
                        _gm.EndTime = DateTime.Now;
                        _gm.EnteringTime = DateTime.Now;
                        _gm.Memo = string.Empty;

                        LayoutRoot.DataContext = _gm;
                }

                private void Window_OnMouseDown(object sender, MouseButtonEventArgs e)
                {
                        if (e.ChangedButton == MouseButton.Left)
                        {
                                this.DragMove();
                        }
                }

               
                private void AcceptButton_Click(object sender, RoutedEventArgs e)
                {
                        if (String.IsNullOrEmpty(_gm.VisitContact.Id))
                        {
                                MessageBox.Show("名称不能为空");
                                return;
                        }


                        try
                        {
                                Visit visit = _gm.TravelFlow.CreateVisit(_gm.VisitContact.Id,
                                                                         _gm.StartTime,
                                                                         _gm.EndTime,
                                                                         _gm.VisitVisitType,
                                                                         _gm.VisitObjective,
                                                                         _gm.VisitKeyEvents,
                                                                         _gm.VisitEatAmount,
                                                                         _gm.VisitGiftAmount,
                                                                         _gm.Memo);
                                visit.Insert();

                                _gm.Visits.Add(visit);
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                return;
                        }

                        finally
                        {
                        }

                        Close();
                }                
        }
}